home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / By the Book / Mac C Primer V2 / 3.3 - MDEF / MDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-04  |  3.8 KB  |  145 lines  |  [TEXT/KAHL]

  1. /************************************************************/
  2. /*                                                            */
  3. /*    MDEF Code from Chapter Three of                            */
  4. /*                                                            */
  5. /*        *** The Macintosh Programming Primer ***            */
  6. /*                                                            */
  7. /*    Copyright 1990, Dave Mark                                */
  8. /*                                                            */
  9. /*    This program demonstrates specific Mac programming        */
  10. /*    techniques.                                                */
  11. /*                                                            */
  12. /************************************************************/
  13.  
  14. #define    MARGIN            2
  15.  
  16.  
  17. /**************************************************************  main  ***/
  18.  
  19. pascal void    main( message, theMenu, menuRectPtr, hitPt, whichItemPtr )
  20. int            message;
  21. MenuHandle    theMenu;
  22. Rect        *menuRectPtr;
  23. Point        hitPt;
  24. int            *whichItemPtr;
  25. {
  26.     short        PICTResID, numPicts, maxH, maxV, i;
  27.     PicHandle    myPicture;
  28.     Rect        r, tempRect;
  29.     int            newItem;
  30.     
  31.     switch( message )
  32.     {
  33.         case mDrawMsg:
  34.             GetNumPicts( theMenu, &PICTResID, &numPicts );
  35.             CalcMaxHV( PICTResID, numPicts, &maxH, &maxV );
  36.             
  37.             r.top = menuRectPtr->top + MARGIN/2;
  38.             r.left = menuRectPtr->left + MARGIN;
  39.             r.bottom = r.top + maxV;
  40.             r.right = r.left + maxH;
  41.                     
  42.             for ( i=0; i<numPicts; i++ )
  43.             {
  44.                 myPicture = GetPicture( PICTResID + i );
  45.                 tempRect = r;
  46.                 CenterPict( myPicture, &tempRect );
  47.                 DrawPicture( myPicture, &tempRect );
  48.                 OffsetRect( &r, 0, maxV + MARGIN );
  49.             }
  50.             break;
  51.         case mChooseMsg:
  52.             GetNumPicts( theMenu, &PICTResID, &numPicts );
  53.             CalcMaxHV( PICTResID, numPicts, &maxH, &maxV );
  54.             
  55.             if ( PtInRect( hitPt, menuRectPtr ) )
  56.             {
  57.                 newItem = ( (hitPt.v - menuRectPtr->top) / (maxV + MARGIN) ) + 1;
  58.                 if ( ( *whichItemPtr > 0 ) && ( *whichItemPtr != newItem ) )
  59.                 {
  60.                     r = *menuRectPtr;
  61.                     r.top += ( (*whichItemPtr-1) * (MARGIN + maxV) );
  62.                     r.bottom = r.top + maxV + MARGIN;
  63.                     InvertRect( &r );
  64.                 }
  65.                 
  66.                 if ( *whichItemPtr != newItem )
  67.                 {
  68.                     *whichItemPtr = newItem;
  69.                     r = *menuRectPtr;
  70.                     r.top += ( (*whichItemPtr-1) * (MARGIN + maxV) );
  71.                     r.bottom = r.top + maxV + MARGIN;
  72.                     InvertRect( &r );
  73.                 }
  74.             }
  75.             else if ( *whichItemPtr > 0 )
  76.             {
  77.                 r = *menuRectPtr;
  78.                 r.top += ( (*whichItemPtr-1) * (MARGIN + maxV) );
  79.                 r.bottom = r.top + maxV + MARGIN;
  80.                 InvertRect( &r );
  81.                 *whichItemPtr = 0;
  82.             }
  83.             break;
  84.         case mSizeMsg:
  85.             GetNumPicts( theMenu, &PICTResID, &numPicts );
  86.             CalcMaxHV( PICTResID, numPicts, &maxH, &maxV );
  87.             (**theMenu).menuWidth = maxH + 2 * MARGIN;
  88.             (**theMenu).menuHeight = (maxV + MARGIN) * numPicts;
  89.             break;
  90.     }
  91. }
  92.  
  93.  
  94. /******************************** CenterPict *********/
  95.  
  96. CenterPict( thePicture, myRectPtr )
  97. PicHandle    thePicture;
  98. Rect        *myRectPtr;
  99. {
  100.     Rect    windRect, pictureRect;
  101.     
  102.     windRect = *myRectPtr;
  103.     pictureRect = (**( thePicture )).picFrame;
  104.     myRectPtr->top = (windRect.bottom - windRect.top - (pictureRect.bottom - pictureRect.top))
  105.         / 2 + windRect.top;
  106.     myRectPtr->bottom = myRectPtr->top + (pictureRect.bottom - pictureRect.top);
  107.     myRectPtr->left = (windRect.right - windRect.left - (pictureRect.right - pictureRect.left))
  108.         / 2 + windRect.left;
  109.     myRectPtr->right = myRectPtr->left + (pictureRect.right - pictureRect.left);
  110. }
  111.  
  112.  
  113. /**************************************************************  CalcMaxHV  ***/
  114.  
  115. CalcMaxHV( PICTResID, numPicts, hPtr, vPtr )
  116. short        PICTResID, numPicts, *hPtr, *vPtr;
  117. {
  118.     short        i;
  119.     Rect        r;
  120.     PicHandle    myPicture;
  121.     
  122.     *hPtr = 0;
  123.     *vPtr = 0;
  124.     for ( i=0; i<numPicts; i++ )
  125.     {
  126.         myPicture = GetPicture( PICTResID + i );
  127.         r = (**myPicture).picFrame;
  128.         
  129.         if ( r.bottom - r.top > *vPtr )
  130.             *vPtr = r.bottom - r.top;
  131.         if ( r.right - r.left > *hPtr )
  132.             *hPtr = r.right - r.left;
  133.     }
  134. }
  135.  
  136.  
  137. /**************************************************************  GetNumPicts  ***/
  138.  
  139. GetNumPicts( theMenu, baseIDPtr, numPictsPtr )
  140. MenuHandle    theMenu;
  141. short        *baseIDPtr, *numPictsPtr;
  142. {
  143.     *baseIDPtr = HiWord((**theMenu).enableFlags);
  144.     *numPictsPtr = LoWord((**theMenu).enableFlags);
  145. }